﻿Public Class CLS_IE

    Dim iPercentageComplete As Integer

    Public WithEvents oIE As InternetExplorer

    ' Update remote database with browser ----------------------------------------------------------------------------------------------------

    Public Sub updateWithBrowser(ByVal sURL As String, ByVal sPostData As String)

        On Error Resume Next

        logMessage( _
            iDEBUG, _
            "CLS_IE", _
            "updateWithBrowser method call", _
            "URL = " & sURL & _
                " | POST data = " & sPostData)

        If oIE Is Nothing Then

            On Error Resume Next

            oIE = New InternetExplorer

            If Err.Number <> 0 Then

                logMessage( _
                    iERROR, _
                    "CLS_IE", _
                    "Could not create InternetExplorer object", _
                    , _
                    Err.Number, _
                    Err.Description)

                MsgBox("Could not update records" & _
                    vbCrLf & _
                    "Reason : could not create Internet Explorer object")

                DLGupdating.terminate()

                Exit Sub

            Else

                logMessage( _
                    iDEBUG, _
                    "CLS_IE", _
                    "Created InternetExplorer object")

            End If

        End If

        ' Set display properties for IE --------------------------------------------------------------------------------

        oIE.Visible = False

        oIE.AddressBar = False
        oIE.Toolbar = False
        oIE.MenuBar = False
        oIE.StatusBar = True

        oIE.StatusText = "Updating"

        oIE.Width = 300
        oIE.Height = 300

        Dim bPostData() As Byte

        bPostData = getPostData(sPostData)

        oIE.Navigate2(sURL, , , bPostData, "Content-Type:application/x-www-form-urlencoded")

        logMessage( _
            iINFO, _
            "CLS_IE", _
            "Navigate2 method of InternetExplorer object called", _
            "URL = " & sURL & _
                " | POST data = " & sPostData & _
                " | From file ? " & MODweb.bUpdatedFromFile)

        DLGupdating.init( _
            "Updating Remote Database", _
            "Please wait while the remote database is updated", _
            Me)

    End Sub

    ' Is called when update is comepleted, regardless of success ----------------------------------------------------------------------------

    Private Sub updateComplete(ByVal oWindow As Object)

        On Error Resume Next

        Dim sSuccess As String

        Dim oIEdoc

        On Error Resume Next

        oIEdoc = oWindow.Document

        If Err.Number <> 0 Then

            logMessage( _
                iWARNING, _
                "CLS_IE", _
                "Could not get IE.document object after documentComplete event", _
                Err.Number, _
                Err.Description)

        Else

            logMessage( _
                iDEBUG, _
                "CLS_IE", _
                "Got IE.document object after documentComplete event")

        End If

        On Error Resume Next

        sSuccess = LCase(oIEdoc.successStatus)

        If Err.Number <> 0 Then

            logMessage( _
                iWARNING, _
                "CLS_IE", _
                "Could not get successStatus property of IE.document object", _
                "InnerHTML of body = " & oIE.Document.body.innerHTML, _
                Err.Number, _
                Err.Description)

        Else

            logMessage( _
                iINFO, _
                "CLS_IE", _
                "successStatus property of IE.document object returned as : " & sSuccess)

        End If

        If sSuccess = "success" Then

            MsgBox("All records have been updated successfully")

            If MODweb.bUpdatedFromFile = True Then

                Dim fso As Scripting.FileSystemObject

                Dim sBackupPath As String

                fso = New Scripting.FileSystemObject

                sBackupPath = MODweb.sRemoteUpdateBackupDirPath & "records_" & MODapplicationMain.getUniqueTimeStamp

                If Not fso.FolderExists(MODweb.sRemoteUpdateBackupDirPath) Then fso.CreateFolder(MODweb.sRemoteUpdateBackupDirPath)

                fso.MoveFile(MODweb.sRemoteUpdateRecordsFilePath, sBackupPath)

                If Err.Number <> 0 Then

                    fso.CopyFile(MODweb.sRemoteUpdateRecordsFilePath, sBackupPath)

                End If

                MODweb.bUpdatedFromFile = False

            Else

                MODweb.resetStatementArray()

            End If

            FRMmain.bRemoteSaved = True

        Else

            MsgBox("The records could not be updated." & vbCrLf & _
                "It is recommended that you save the records for updating later")

            FRMmain.bRemoteSaved = False

        End If

        oIE.Quit()

        oIE = Nothing

    End Sub

    ' Fires when the document is completely loaded ----------------------------------------------------------------------------
    ' This event will not fire in IE 5.5 or previous versions, when Visible property is false --------------------------

    Private Sub oIE_DocumentComplete(ByVal pDisp As Object, ByVal URL As Object)

        On Error Resume Next

        DLGupdating.terminate()

        updateComplete(pDisp)

    End Sub

    ' this event is always fired after a navigation starts. Any animation or "busy" indication that the container needs to display should be connected to this event. - MSDN

    'Private Sub oIE_DownloadComplete()

    '    On Error Resume Next

    '    DLGupdating.terminate

    '   updateComplete

    'End Sub

    Private Sub oIE_NavigateError(ByVal pDisp As Object, ByVal URL As Object, ByVal Frame As Object, ByVal StatusCode As Object, ByVal Cancel As Boolean)

        On Error Resume Next

        DLGupdating.terminate()

    End Sub

    ' Quit event for browser -------------------------------------------------------------------------------------------------------------------

    Private Sub oIE_OnQuit()

        On Error Resume Next

        DLGupdating.terminate()

        oIE = Nothing

    End Sub

    Private Sub oIE_ProgressChange(ByVal Progress As Long, ByVal ProgressMax As Long)


    End Sub

End Class
